/* 
 * File:   Pile.cpp
 * Author: Gab'z
 * 
 * Created on 8 mai 2012, 17:09
 */

#include "Pile.h"

Pile::~Pile() {
    for (std::list<Constante*>::iterator it = _constanteStack.begin(); it != _constanteStack.end(); ++it) {
        delete *it;
    }
    _constanteStack.clear();
}

int Pile::size() const {
    return _constanteStack.size();
}

void Pile::push(const Constante* c) {
    _constanteStack.push_front(c->clone());
}

const Constante* Pile::top() {
    return _constanteStack.front();
}

void Pile::pop() {
    if (!_constanteStack.empty()) {
        delete _constanteStack.front();
        _constanteStack.pop_front();
    }
}

const Constante* Pile::getConstante(unsigned int n) const throw (std::out_of_range) {
    unsigned int i = 1;
    Constante* c = 0;
    for (std::list <Constante*>::const_iterator it = _constanteStack.begin(); it != _constanteStack.end(); ++it) {
        if (n == i) {
            c = *it;
        }
        i++;
    }
    if (c == 0) throw std::out_of_range("Pas assez de constante dans la pile");
    return c;
}